package com.qlm.interceptor;

import java.util.UUID;

import com.jfinal.aop.Before;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;
import com.jfinal.weixin.sdk.api.JsTicketApi.JsApiType;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.qlm.entity.User;
import com.qlm.service.UserService;

/**
 * 微信端controller直接继承该controller
 * @author wangzih
 *
 */
@Before(WeixinInterceptor.class)
public class WeixinController extends ApiController{
	
	@Override
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();
		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("appid"));
		ac.setAppSecret(PropKit.get("appsecret"));

		/**
		 * 是否对消息进行加密，对应于微信平台的消息加解密方式： 1：true进行加密且必须配置 encodingAesKey
		 * 2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey",
				"setting it in config file"));
		return ac;
	}

	/**
	 * 供子类获取openId
	 * @return
	 */
	protected String getOpenId() {
		return getUser().getStr(UserService.OPENID);
	}
	
	/**
	 * 供子类获取昵称
	 * @return
	 */
	protected String getNickName() {
		return getUser().getStr(UserService.NICK_NAME);
	}
	
	/**
	 * 供子类获取头像
	 * @return
	 */
	protected String getHeadImg() {
		return getUser().getStr(UserService.HEAD_PIC);
	}
	
	/**
	 * 供子类获取用户
	 * @return
	 */
	protected User getUser() {
		User user = getSessionAttr(WeixinInterceptor.user_key);
		return user;
	}
	
	/**
	 * 获取用户某个属性
	 */
	protected <T> T getUserAttr(String name) {
		return getUser().get(name);
	}
	
	protected void setJsTicket() {
		ApiConfigKit.setThreadLocalApiConfig(getApiConfig());
		JsTicket jsApiTicket = JsTicketApi.getTicket(JsApiType.jsapi);
		String jsapi_ticket = jsApiTicket.getTicket();
		String nonce_str = create_nonce_str();
		// 注意 URL 一定要动态获取，不能 hardcode.
		String url = "http://" + getRequest().getServerName() // 服务器地址
				// + ":"
				// + getRequest().getServerPort() //端口号
				+ getRequest().getContextPath() // 项目名称
				+ getRequest().getServletPath();// 请求页面或其他地址
		String qs = getRequest().getQueryString(); // 参数
		if (qs != null) {
			url = url + "?" + (getRequest().getQueryString());
		}
		String timestamp = create_timestamp();
		// 这里参数的顺序要按照 key 值 ASCII 码升序排序
		//注意这里参数名必须全部小写，且必须有序
		String  str = "jsapi_ticket=" + jsapi_ticket +
        "&noncestr=" + nonce_str +
        "&timestamp=" + timestamp +
        "&url=" + url;
		String signature = HashKit.sha1(str);
		setAttr("appId", ApiConfigKit.getApiConfig().getAppId());
		setAttr("nonceStr", nonce_str);
		setAttr("timestamp", timestamp);
		setAttr("url", url);
		setAttr("signature", signature);
		setAttr("jsapi_ticket", jsapi_ticket);
	}
	
	private static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}
	
	private static String create_nonce_str() {
		return UUID.randomUUID().toString();
	}
}
